// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import 'e-ui/lib/styles/index.css'
import 'e-ui/lib/styles/components/index.css'
import './styles/index.scss'
// import { i18n, I18N_CONFIG } from './i18n'
require('./i18n')
import * as ElementUI from '@element-ui'
import * as EUI from '@e-ui/index.js'
import * as filters from './filters' // 全局vue filter
import * as GlobalUI from 'src/components'
import VueAMap from 'vue-amap'
import VueClipboards from 'vue-clipboards'
import globalMixin from 'utils/mixins/global'
import './directives'
import './utils/polyfill'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import EL_EN from 'element-ui/lib/locale/lang/en'
import EL_ZH_CN from 'element-ui/lib/locale/lang/zh-CN'

Vue.use(VueClipboards)
Vue.use(VueAMap)
Vue.use(EUI)
Vue.use(GlobalUI)
Vue.mixin(globalMixin)
Vue.use(ElementUI)

//浏览兼容处理
let triggerEvent = (node) => { // 触发input事件
  let event = document.createEvent('HTMLEvents')
  event.initEvent('input', true, true)
  node.dispatchEvent(event)
}
document.addEventListener('focusout', (e) => {
  let target = e.target
  if (/^(input|textarea)$/i.test(target.nodeName) && target.value && target.getAttribute('maxLength') && target.maxLength < target.value.length) { // 异常处理
    target.value = target.value.substring(0, target.maxLength)
    if (target.dispatchEvent) {
      triggerEvent(target)
    }
  }
}, false)
if (document.all && /MSIE 9./i.test(navigator.appVersion)) { // ie 9兼容处理
  let $timer = null
  let handle = (e) => {
    $timer && clearTimeout($timer)
    let target = e.target
    if (target.dispatchEvent) {
      $timer = setTimeout(() => {
        triggerEvent(target)
      }, 150)
    }
  }
  document.addEventListener('keyup', handle, false)
  document.addEventListener('focusout', handle, false)
}

const GaoDeMapKey = 'a4f8c47ba3d3cf9c83c49c901619568a'
VueAMap.initAMapApiLoader({
  key: GaoDeMapKey,
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.Geocoder',
    'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor', 'Geolocation', 'Geocoder']
})
// register global utility filters.
Object.keys(filters.default).forEach(key => {
  Vue.filter(key, filters.default[key])
})

// 生产环境错误日志
if (process.env === 'production') {
  Vue.config.errorHandler = function (err, vm) {
    console.log(err, window.location.href, vm)
    /*
    errLog.pushLog({
      err,
      url: window.location.href,
      vm
    })
    */
  }
}
/*
页面进度条控制
*/
router.beforeEach((to, from, next) => {
  store.commit('LOADING', true)
  next()
})
router.afterEach(() => {
  store.loader.timer = setTimeout(() => {
    store.commit('LOADING', false)
  }, 200)
  store.loader.onComplete = () => {
    store.commit('LOADING', false)
  }
})

new Vue({
  // i18n,
  router,
  store,
  render: h => h(App)
}).$mount('#app')
